package com.zhicheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhicheng.mapper.BannerMapper;
import com.zhicheng.pojo.AddressBook;
import com.zhicheng.pojo.Banner;
import com.zhicheng.pojo.result.PageResult;
import com.zhicheng.service.BannerService;
import com.zhicheng.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Override
    public PageResult<Banner> getBanner(int page, int pageSize) {
        PageResult<Banner> pageResult = new PageResult<>();
        PageHelper.startPage(page,pageSize);
        List<Banner> bannerList = bannerMapper.getBanner();
        Page<Banner> p = (Page<Banner>) bannerList;
        pageResult.setTotal(p.getTotal());
        pageResult.setItems(p.getResult());
        pageResult.setPages(p.getPages());
        pageResult.setPage(p.getPageNum());
        pageResult.setPageSize(p.getPageSize());
        return pageResult;
    }

    @Override
    public void addBanner(Banner banner) {
        banner.setCreateTime(LocalDateTime.now());
        banner.setUpdateTime(LocalDateTime.now());
        bannerMapper.addBanner(banner);
    }

    @Override
    public void updateBanner(Banner banner) {
        banner.setUpdateTime(LocalDateTime.now());
        bannerMapper.updateBanner(banner);
    }

    @Override
    public void deleteBanner(Long id) {
        bannerMapper.deleteBanner(id);
    }
}
